#!/sbin/openrc-run

# This script is used to manage k3s via OpenRC.

# shellcheck shell=ksh

if [ -f /etc/environment ]; then source /etc/environment; fi

# ENGINE configuration variable is either "moby" or "containerd"
ENGINE="${ENGINE:-containerd}"

depend() {
  after network-online
  want cgroups
  if [ "${ENGINE}" == "moby" ]; then
    need docker
  fi
}

start_pre() {
  rm -f /tmp/k3s.*
  checkpath --file --mode 0644 --owner root "${output_log_unquoted}" "${error_log_unquoted}"
}

supervisor=supervise-daemon
name=k3s
command=/usr/local/bin/k3s
command_args="server --https-listen-port ${PORT} ${ADDITIONAL_ARGS:-}"
if [ "${ENGINE}" == "moby" ]; then
  command_args="${command_args} --docker"
fi
command_args="${command_args} ${K3S_EXEC:-}"
K3S_LOGFILE="${K3S_LOGFILE:-${LOG_DIR:-/var/log}/${RC_SVCNAME}.log}"
output_log_unquoted="${K3S_OUTFILE:-${K3S_LOGFILE}}"
output_log="'${output_log_unquoted}'"
error_log_unquoted="${K3S_ERRFILE:-${K3S_LOGFILE}}"
error_log="'${error_log_unquoted}'"

pidfile="/var/run/k3s.pid"
respawn_delay=5
respawn_max=0

set -o allexport
